<?php

class openapi_utils_xmlutil {

    public function arrtoxml($arr, $dom = 0, $item = 0) {
        if (! $dom) {
            $dom = new DOMDocument("1.0");
        }
        if (! $item) {
            $item = $dom->createElement("root");
            $dom->appendChild($item);
        }
        if (gettype($arr) == 'Object') {
            $arr = $this->objectToArray($arr);
        }
        foreach ($arr as $key => $val) {
            $itemx = $dom->createElement(is_string($key) ? $key : "item");
            $item->appendChild($itemx);
            if (! is_array($val)) {
                $text = $dom->createTextNode($val);
                $itemx->appendChild($text);
            } else {
                $this->arrtoxml($val, $dom, $itemx);
            }
        }
        return $dom->saveXML();
    }

    private function objectToArray($e) {
        $e = (array) $e;
        foreach ($e as $k => $v) {
            if (gettype($v) == 'resource')
                return;
            if (gettype($v) == 'object' || gettype($v) == 'array')
                $e[$k] = (array) $this->objectToArray($v);
        }
        return $e;
    }
}
